package com.test.applicationexitreasontest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.ActivityManager;
import android.app.ApplicationExitInfo;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActivityManager activityManager = getActivityManager();
        List<ApplicationExitInfo> historicalProcessExitReasons = activityManager
                .getHistoricalProcessExitReasons(getPackageName(), 0, 10);

        RecyclerView recyclerView = findViewById(R.id.recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new MyRecyclerAdapter(historicalProcessExitReasons,this));

        TextView textView = findViewById(R.id.textView2);
        textView.setText("List<ApplicationExitInfo> size:"+historicalProcessExitReasons.size());
        textView.setOnClickListener(v -> {
            int i = 1/0;//在app中制造崩溃效果，重启应用查看退出原因
        });

    }

    private ActivityManager getActivityManager() {
        return (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    }
}